package com.kaka.bus.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kaka.bus.R;
import com.kaka.bus.bean.EvaluateBean;
import com.kaka.bus.util.U;

import java.util.List;

/**
 * Created by OnionMac on 2018/1/17.
 */

public class EvaluateAdapter extends BaseQuickAdapter<EvaluateBean,BaseViewHolder> {

    private Context mContext;

    private static final String SINGLE = "1";
    private static final String MORE = "2";

    public EvaluateAdapter(int layoutResId, @Nullable List<EvaluateBean> data, Context context) {
        super(layoutResId, data);
        mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, EvaluateBean item) {

        helper.setText(R.id.item_evaluate_name,item.getName())
                .setText(R.id.item_evaluate_number,item.getSerial());

        if("这是说明".equals(item.getDescription())){
            helper.setVisible(R.id.item_evaluate_des,false);
        }else{
            helper.setText(R.id.item_evaluate_des, U.connectString("(",item.getDescription(),")")).setVisible(R.id.item_evaluate_des,true);
        }

        RecyclerView recy = helper.getView(R.id.item_evaluate_recy);
        recy.setLayoutManager(new GridLayoutManager(mContext,2));
        EvaluateItemAdapter evaluateItemAdapter = new EvaluateItemAdapter(R.layout.item_evaluate_item, item.getList(), mContext);
        recy.setAdapter(evaluateItemAdapter);

        evaluateItemAdapter.setOnItemClickListener((adapter, view, position) -> {
            List<EvaluateBean.ListBean> list = item.getList();
                if(item.getStyle().equals(SINGLE)){
                    /**
                     * 单选限制
                     */
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).setCheck(i == position);
                    }
                    this.notifyDataSetChanged();
                }else if(item.getStyle().equals(MORE)){
                    /**
                     * 没有限制
                     */
                    EvaluateBean.ListBean items = list.get(position);
                    items.setCheck(!items.isCheck());
                    this.notifyDataSetChanged();
                }
        });
    }
}
